import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  props: {
    selectBol: true,
    currentAddress: String
  },
  data() {
    return {
      lastIndex: -1,
      defaultIndex: 0,
      activeName: "first",
      checkList: [],
      datasource: [],
      show_add_address: true
    };
  },

  methods: {
    copyLink(item){
      console.log(item,'item===');
        return `收件人:${item.username}\n手机号码:${item.mobile}\n所在地区:${ item.province }${item.city }${item.district }${item.street?item.street:''}\n详细地址:${item.address?item.address:''}`;
    },
    // 复制邀请码
    onCopy: function(e) {
      this.$toast({
        message: "复制成功",
        duration: 1000
      });
    },
    onError: function(e) {
      this.$toast({
        message: "复制失败",
        duration: 1000
      });
    },
    selectBolfun: function() {
      this.selectBol = true;
    },
    //设置默认地址
    setDefaultAddress(itemIndex) {
      if (this.selectBol) {
        this.postData(this.datasource[itemIndex].id, itemIndex);
      }
    },
    selectedAddress(index) {
      if (this.checkList[index]) return;
      this.postData(this.datasource[index].id, index);
    },
    //添加地址
    addendAddress() {
      let defaultIs = true;
      if (this.datasource.length > 0) {
        for (let i = 0; i < this.datasource.length; i++) {
          if (this.datasource[i].isdefault == 1) {
            defaultIs = false;
          }
        }
      }
      this.$router.push(this.fun.getUrl("appendAddress", { isdefault: defaultIs }));
    },
    //删除地址
    deleteHandler(item, itemIndex) {
      this.$dialog
        .confirm({ message: "确定删除此收货地址?" })
        .then(() => {
          //删除当前选中的地址
          console.log(item);
          $http.get("member.member-address.destroy", { address_id: item.id }, true).then(
            response => {
              this.datasource.splice(this.datasource.indexOf(item), 1);
              this.checkList.splice(itemIndex, 1);
            },
            response => {
              this.$dialog.alert({ message: response.msg });
            }
          );
        })
        .catch(() => {});
    },
    //修改收货地址
    alterHandler(itemIndex) {
      let query = {};
      if(this.$route.query.tag == 'makeAppointment'){
        query.tag = 'makeAppointment';
      }
      this.$router.push(this.fun.getUrl("alterAddress", { model: this.datasource[itemIndex] },query));
    },
    getAddress() {
      $http.get("member.member-address.page", {}, ".").then(
        response => {
          if (response.result == 1) {
            this.page = 1;
            this.total_page = response.data.last_page;
            this.isLoadMore = true;
            for (var i = 0; i < response.data.data.length; i++) {
              var item = response.data.data[i];
              if (item.isdefault == 1) {
                this.$set(this.checkList, i, true);
              } else {
                this.$set(this.checkList, i, false);
              }
            }
            this.datasource = response.data.data;
          } else {
            console.log(response.msg);
          }
        },
        function(response) {
          // error callback
        }
      );
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        this.finished = true;
        return;
      } else {
        let _json = {};
        this.page = this.page + 1;
        _json.page = this.page;

        $http.get('member.member-address.page', _json, "加载中").then((response) => {
          this.isLoadMore = true;
          if (response.result === 1) {
            var myData = response.data.data;
            let current_length = this.datasource.length;
            this.datasource = this.datasource.concat(myData); //数组拼接
            this.$forceUpdate();
            for (var i = 0; i < response.data.data.length; i++) {
              var item = response.data.data[i];
              if (item.isdefault == 1) {
                this.$set(this.checkList, i + current_length, true);
              } else {
                this.$set(this.checkList, i + current_length, false);
              }
            }
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
          }
        },
        function (response) {
          // error callback
        }
        );
      }
    },
    //设置默认地址的
    postData(address_id, index) {
      $http.get("member.member-address.setDefault", { address_id: address_id }).then(
        response => {
          if (response.result == 1) {
            this.$dialog.alert({ message: response.msg,confirmButtonColor:'var(--themeBaseColor)' });
            this.datasource[index].isdefault = true;
            this.setDefaultAddressSuccessful(index);
            this.selectBol = false;
          } else {
            let checked = !this.checkList[index];
            this.$set(this.checkList, index, checked);
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // error callback
        }
      );
    },
    setDefaultAddressSuccessful(itemIndex) {
      if (this.lastIndex < 0) {
        //首次进入页面
        this.lastIndex = itemIndex;
        for (let i = 0; i < this.checkList.length; i++) {
          this.$set(this.checkList, i, false);
        }
      } else {
        this.$set(this.checkList, this.lastIndex, false);
        this.lastIndex = itemIndex;
      }
      this.$set(this.checkList, itemIndex, true);
    }
  },
  activated() {
    this.datasource = [];
    this.getAddress();
    let yz_basic_info = JSON.parse(window.localStorage.getItem("yz_basic_info"));
    if (yz_basic_info.plugin_setting && yz_basic_info.plugin_setting.address_code) {
      let address_code = yz_basic_info.plugin_setting.address_code;
      this.show_add_address = address_code.show_add_address;
    }
  },
  components: {}
};
